LL_DAC_Init and LL_DAC_DeInit APIs migration

The LL initialization and de-initialization functions previously available in the stm32tnxx_ll_dac.c files within HAL1 are no longer present in HAL2. In HAL2, all LL C files ( stm32tnxx_ll_dac.c) have been removed, leaving only the LL header files ( stm32tnxx_ll_dac.h) that contain static inline functions.

If users need to initialize or de-initialize DAC using the LL layer, they have two methods:

  • Utilize STM32CubeMX2 Code Generation: Rely on STM32CubeMX2 to generate code with the LL layer for DAC initialization and de-initialization.

  • Replace manually the initialization and de-initialization functionsReplace the LL_DAC_Init and LL_DAC_DeInit functions

    manually with equivalent sequences using the static inline functions provided in the stm32tnxx_ll_dac.h file, tailored to their specific configuration and requirements.

This approach ensures that the initialization and de-initialization sequences are precisely addressing the given use case based on the LL static inline functions provided in the stm32tnxx_ll_dac.h header file. The sequences offer improved footprint optimization compared to the previous LL initialization functions found in stm32tnxx_ll_dac.c.

Related concept:

Method 1: Utilize STM32CubeMX2 Code Generation

LL DAC configuration panel

  • Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

  • The LL library is used for DAC initialization, with resource initialization code set to be generated and called within the project.

Project configuration panel

LL DAC main features configuration

Configure the DAC main features.

DAC main feature configuration

LL DAC channels configuration

  • Configure the DAC channels basic features.

  • Configure the alignment, Trigger, Output buffer, Output connection and the data sign format.

DAC basic channel feature configuration

LL GPIO configuration

Configure GPIO pins.

DAC GPIO configuration

LL DMA configuration

Configure the DMA for data reception as needed.

DAC DMA configuration

LL NVIC configuration

Configure the NVIC in case of an interrupt.

DAC NVIC configuration

Generated DAC initialization sequence

The DAC initialization and de-initialization sequences are generated in the mx_dac_ll_init and mx_dac_ll_deinit functions in mx_dac.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

STM32CubeMX2 Generated : DAC initialization sequence

Topic

Code Snippet HAL1

Code Snippet HAL2

Initialization and configuration sequence

static void MX_DAC1_Init(void)
{

  LL_DAC_InitTypeDef DAC_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  LL_RCC_SetADCDACClockSource(LL_RCC_ADCDAC_CLKSOURCE_HSI);
  LL_RCC_SetDAC1ClockSource(LL_RCC_DAC1_CLKSOURCE_LSI);

  LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_DAC1);

  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);



  GPIO_InitStruct.Pin = LL_GPIO_PIN_4;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  LL_DAC_SetHighFrequencyMode(DAC1, LL_DAC_HIGH_FREQ_MODE_ABOVE_160MHZ);

  LL_DAC_SetSignedFormat(DAC1, LL_DAC_CHANNEL_1, LL_DAC_SIGNED_FORMAT_DISABLE);
  DAC_InitStruct.TriggerSource = LL_DAC_TRIG_SOFTWARE;
  DAC_InitStruct.WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE;
  DAC_InitStruct.OutputConnection = LL_DAC_OUTPUT_CONNECT_GPIO;
  DAC_InitStruct.OutputMode = LL_DAC_OUTPUT_MODE_NORMAL;
  LL_DAC_Init(DAC1, LL_DAC_CHANNEL_1, &DAC_InitStruct);
  LL_DAC_EnableTrigger(DAC1, LL_DAC_CHANNEL_1);
  LL_DAC_DisableDMADoubleDataMode(DAC1, LL_DAC_CHANNEL_1);

}
DAC_TypeDef *mx_dac1_ll_init(void)
{
  LL_DAC_SetHighFrequencyMode(DAC1, LL_DAC_HIGH_FREQ_MODE_ABOVE_160MHZ);


  LL_DAC_EnableTrigger(DAC1, LL_DAC_CHANNEL_1);
  LL_DAC_SetTriggerSource(DAC1, LL_DAC_CHANNEL_1, LL_DAC_TRIGGER_SOFTWARE);

  LL_DAC_ConfigOutput(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_MODE_NORMAL, LL_DAC_OUTPUT_BUFFER_ENABLE, LL_DAC_OUTPUT_CONNECT_EXTERNAL);
  LL_DAC_SetSignedFormat(DAC1, LL_DAC_CHANNEL_1, LL_DAC_SIGN_FORMAT_UNSIGNED);


  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  LL_DAC_DisableDMADoubleDataMode(DAC1, LL_DAC_CHANNEL_1);

  /* Activate the Pull-up, Pull-down resistor, or No pull for the current IO */
  LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_4, LL_GPIO_PULL_NO);
  /* Configure IO direction mode (Input, Output, Alternate or Analog) */
  LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_ANALOG);


  LL_DAC_SetHighFrequencyMode(DAC1, LL_DAC_HIGH_FREQ_MODE_ABOVE_160MHZ);

  LL_DAC_SetSignedFormat(DAC1, LL_DAC_CHANNEL_1, LL_DAC_SIGN_FORMAT_UNSIGNED);
  LL_DAC_SetTriggerSource(DAC1, LL_DAC_CHANNEL_1, LL_DAC_TRIGGER_SOFTWARE);
  LL_DAC_EnableTrigger(DAC1, LL_DAC_CHANNEL_1);
  LL_DAC_ConfigOutput(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_MODE_NORMAL, LL_DAC_OUTPUT_BUFFER_ENABLE, LL_DAC_OUTPUT_CONNECT_EXTERNAL);




  return DAC1;
}

Deinitialization sequence

void MX_DAC1_DeInit(void)
{
  if (LL_DAC_DeInit(DAC1) != HAL_OK)
  {
    Error_Handler();
  }
}
void mx_dac1_deinit(void)
{
  LL_DAC_Disable(DAC1, LL_DAC_CHANNEL_1);  LL_DAC_Disable(DAC1, LL_DAC_CHANNEL_2);

  LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_DAC1);

  LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_DAC1);
  LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_DAC1);

  /* ### GPIO deinitialization of DAC1: DAC1_OUT1,DAC1_OUT2 ########################### */

  /* Configure IO in Analog Mode */
  LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_ANALOG);
  LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_5, LL_GPIO_MODE_ANALOG);

  /* Configure the default Alternate Function in current IO */
  LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_4, LL_GPIO_AF_0);
  LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_5, LL_GPIO_AF_0);

  /* Configure the default value for IO Speed */
  LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_4, LL_GPIO_SPEED_FREQ_LOW);
  LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_5, LL_GPIO_SPEED_FREQ_LOW);

  /* Configure the default value IO Output Type */
  LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_4 | LL_GPIO_PIN_5, LL_GPIO_OUTPUT_PUSHPULL);

  /* Deactivate the Pull-up and Pull-down resistor for the current IO */
  LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_4, LL_GPIO_PULL_NO);
  LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_5, LL_GPIO_PULL_NO);

  /* Reset the IO output state */
  LL_GPIO_WriteOutputPin(GPIOA, LL_GPIO_PIN_4 | LL_GPIO_PIN_5, LL_GPIO_PIN_RESET);

}

Method 2: Replace manually the initialization and de-initialization functions

LL_DAC_Init() API migration

According to the use-case, implement the precise sequence call based on the LL static inline functions provided in the stm32tnxx_ll_dac.h header file.

DAC Mode Comparison between HAL1 and HAL2

HAL1

HAL2

Case of WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE

LL_DAC_InitTypeDef dac_InitStruct;

dac_InitStruct.TriggerSource            = LL_DAC_TRIG_SOFTWARE;
dac_InitStruct.WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NOISE;
dac_InitStruct.WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
LL_DAC_Init(DAC1, &dac_InitStruct);
LL_DAC_SetTriggerSource(DAC1, LL_DAC_CHANNEL_1,
                        LL_DAC_TRIG_SOFTWARE);
LL_DAC_SetWaveAutoGeneration(DAC1, LL_DAC_CHANNEL_1,
                             LL_DAC_WAVE_AUTO_GENERATION_NOISE);
LL_DAC_SetWaveNoiseLFSR(DAC1, LL_DAC_CHANNEL_1,
                        LL_DAC_NOISE_LFSR_UNMASK_BIT0);

Case of WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE

LL_DAC_InitTypeDef dac_InitStruct;

dac_InitStruct.TriggerSource      = LL_DAC_TRIG_SOFTWARE;
dac_InitStruct.WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE;
dac_InitStruct.OutputBuffer       = LL_DAC_OUTPUT_BUFFER_ENABLE;
dac_InitStruct.OutputConnection   = LL_DAC_OUTPUT_CONNECT_GPIO;
dac_InitStruct.OutputMode         = LL_DAC_OUTPUT_MODE_NORMAL;
LL_DAC_Init(DAC1, &dac_InitStruct);
LL_DAC_SetTriggerSource(DAC1, LL_DAC_CHANNEL_1,
                        LL_DAC_TRIG_SOFTWARE);
LL_DAC_SetWaveAutoGeneration(DAC1, LL_DAC_CHANNEL_1,
                             LL_DAC_WAVE_AUTO_GENERATION_NONE);
LL_DAC_SetOutputBuffer(DAC1,
                       LL_DAC_CHANNEL_1,
                       LL_DAC_OUTPUT_BUFFER_ENABLE);
LL_DAC_SetOutputConnection(DAC1,
                           LL_DAC_CHANNEL_1,
                           LL_DAC_OUTPUT_CONNECT_GPIO);
LL_DAC_SetOutputMode(DAC1,
                     LL_DAC_CHANNEL_1,
                     LL_DAC_OUTPUT_MODE_NORMAL);

LL_DAC_DeInit() API migration

According to the used DAC1, Implement the precise calls to LL_AHB3_GRP1_ForceReset() and LL_AHB3_GRP1_ReleaseReset()

Comparison of HAL1 and HAL2

HAL1

HAL2

LL_DAC_DeInit(DAC1)
LL_AHB3_GRP1_ForceReset(LL_AHB3_GRP1_PERIPH_DAC1);
LL_AHB3_GRP1_ReleaseReset(LL_AHB3_GRP1_PERIPH_DAC1);